package com.wiscamp.ninechapters.panels.adapter.resources;

import com.wiscamp.ninechapters.panels.application.command.CreateDictionaryCommand;
import com.wiscamp.ninechapters.panels.application.command.UpdateDictionaryCommand;
import com.wiscamp.ninechapters.panels.application.services.DictionaryAppService;
import com.wiscamp.ninechapters.panels.application.vo.DictionaryVO;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 字典服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/ControlPanels")
@Tag(name = "控制面板-字典信息")
public class DictionaryController extends BaseController {

    private final DictionaryAppService dictionaryAppService;

    public DictionaryController(DictionaryAppService dictionaryAppService) {
        this.dictionaryAppService = dictionaryAppService;
    }

    @GetMapping("Dictionaries/{dictionaryId}")
    @Operation(summary = "通过编号获取字典")
    @HandleResourceException
    public RestResponse<DictionaryVO> getDictionary(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                    @PathVariable("dictionaryId") long dictionaryId) {
        return ResourceUtils.getById(request, response, dictionaryId, dictionaryAppService::getDictionary);
    }


    @GetMapping("Applications/{appId}/Dictionaries/Code/{dictionaryCode}")
    @Operation(summary = "通过代码获取字典")
    @HandleResourceException
    public RestResponse<DictionaryVO> getDictionaryByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                          @PathVariable("appId") long appId, @PathVariable("dictionaryCode") String dictionaryCode) {
        return ResourceUtils.getByIdAndText(request, response, appId, dictionaryCode, dictionaryAppService::getDictionaryByCode);
    }

    @GetMapping("Applications/{appId}/Dictionaries/Name/{dictionaryName}")
    @Operation(summary = "通过名称获取字典列表")
    @HandleResourceException
    public RestResponse<Collection<DictionaryVO>> getDictionariesByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                        @PathVariable("appId") long appId, @PathVariable("dictionaryName") String dictionaryName) {
        return ResourceUtils.getListByIdAndText(request, response, appId, dictionaryName, dictionaryAppService::getDictionariesByName);
    }

    @GetMapping("Applications/{appId}/Dictionaries/Group/{dictionaryGroup}")
    @Operation(summary = "通过分组获取字典列表")
    @HandleResourceException
    public RestResponse<Collection<DictionaryVO>> getDictionariesByGroup(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                         @PathVariable("appId") long appId, @PathVariable("dictionaryGroup") String dictionarGroup) {
        return ResourceUtils.getListByIdAndText(request, response, appId, dictionarGroup, dictionaryAppService::getDictionariesByGroup);
    }

    @PostMapping("Dictionaries/Query")
    @Operation(summary = "分页获取字典列表")
    @HandleResourceException
    public RestResponse<Collection<DictionaryVO>> getDictionariesByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                         @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, dictionaryAppService::getDictionariesByQuery);
    }

    @PostMapping("Dictionaries")
    @Operation(summary = "创建字典")
    @HandleResourceException
    public RestResponse<DictionaryVO> createDictionary(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody CreateDictionaryCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, dictionaryAppService::createDictionary);
    }


    @PutMapping("Dictionaries")
    @Operation(summary = "更新字典")
    @HandleResourceException
    public RestResponse<DictionaryVO> updateDictionary(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody UpdateDictionaryCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, dictionaryAppService::updateDictionary);
    }

    @PutMapping("Dictionaries/Actions/Delete")
    @Operation(summary = "通过编号删除字典")
    @HandleResourceException
    public RestResponse<Boolean> deleteDictionary(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, dictionaryAppService::deleteDictionary);
    }

    @PutMapping("Dictionaries/Actions/Enable")
    @Operation(summary = "通过编号启用字典")
    @HandleResourceException
    public RestResponse<Boolean> enableDictionary(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, dictionaryAppService::enableDictionary);
    }

    @PutMapping("Dictionaries/Actions/Disable")
    @Operation(summary = "通过编号禁用字典")
    @HandleResourceException
    public RestResponse<Boolean> disableDictionary(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, dictionaryAppService::disableDictionary);
    }
}
